package com.woniu.project.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.woniu.project.core.dto.ResultDto;
import com.woniu.project.core.exception.ProjectProductException;
import com.woniu.project.dto.productproperty.ProdcutPropertySelectDto;
import com.woniu.project.params.productproperty.ProdcutPropertyAddParam;
import com.woniu.project.params.productproperty.ProdcutPropertySelectParam;
import com.woniu.project.params.productproperty.ProdcutPropertyUpdateParam;
import com.woniu.project.service.IProductPropertyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lucas
 * @since 2020-10-12
 */
@RestController
@RequestMapping("/productProperty")
@Api(tags="属性管理")
public class ProductPropertyController {
    @Autowired
    private IProductPropertyService productPropertyService;
    @ApiOperation(value="属性查询",notes = "参数均选填")
    //根据是否有id和name进行不同的分页查询
    @GetMapping("/select")
    public ResultDto select(@ModelAttribute ProdcutPropertySelectParam prodcutPropertySelectParam) {

        try {
            IPage<ProdcutPropertySelectDto> productPropertyIpage = productPropertyService.select(prodcutPropertySelectParam);
            return ResultDto.success(200, "查询成功", productPropertyIpage);
        } catch (ProjectProductException e) {
            e.printStackTrace();
            throw e;
        }catch (Exception e) {
            e.printStackTrace();
            throw new ProjectProductException(500, "商品属性查询系统异常");
        }

    }
    @ApiOperation(value="根据商品实体id查询商品属性",notes = "id必填")
    //根据商品实体id查询商品属性
    @GetMapping("/selectByEntityId")
    public ResultDto selectByEntityId(@RequestParam Integer id) {

        try {
            List<ProdcutPropertySelectDto> prodcutPropertySelectDtos = productPropertyService.selectByEntityId(id);
            return ResultDto.success(200, "查询成功", prodcutPropertySelectDtos);
        } catch (ProjectProductException e) {
            e.printStackTrace();
            throw e;
        }catch (Exception e) {
            e.printStackTrace();
            throw new ProjectProductException(500, "商品类型查询系统异常");
        }

    }
    @ApiOperation(value="新增商品属性",notes = "id不填,其他参数均必填")
    //新增商品属性
    @PostMapping("/addProperty")
    public ResultDto addProperty(@Valid @RequestBody ProdcutPropertyAddParam prodcutPropertyAddParam) {
        try {
            int result = productPropertyService.addProperty(prodcutPropertyAddParam);
            if (result == 1) {
                return ResultDto.success(500, "该序号已存在，请重新输入", null);
            }
            return ResultDto.success(200, "新增商品属性成功", null);
        } catch (ProjectProductException e) {
            e.printStackTrace();
            throw e;
        }catch (Exception e) {
            e.printStackTrace();
            throw new ProjectProductException(500, "商品属性新增系统异常");
        }
    }
    @ApiOperation(value="修改商品属性",notes = "参数全必填")
    //修改商品属性
    @PutMapping("/updateProperty")
    public ResultDto updateProperty(@Valid @RequestBody ProdcutPropertyUpdateParam prodcutPropertyUpdateParam) {
        try {
            int result = productPropertyService.updateProperty(prodcutPropertyUpdateParam);
            if (result == 1) {
                return ResultDto.success(500, "该序号已存在，请重新输入", null);
            }
            return ResultDto.success(200, "修改商品属性成功", null);
        } catch (ProjectProductException e) {
            e.printStackTrace();
            throw e;
        }catch (Exception e) {
            e.printStackTrace();
            throw new ProjectProductException(500, "商品属性修改系统异常");
        }
    }
    @ApiOperation(value="根据id删除属性",notes = "id必填")
    //根据id删除属性
    @DeleteMapping("/deleteById/{id}")
    public ResultDto deleteById(@PathVariable Integer id) {
        try {
            productPropertyService.deleteById(id);
            System.out.println("-----------");
            return ResultDto.success(200, "删除商品属性成功", null);
        } catch (ProjectProductException e) {
            e.printStackTrace();
            throw e;
        }catch (Exception e) {
            e.printStackTrace();
            throw new ProjectProductException(500, "商品属性删除系统异常");
        }
    }
}

